package server;
import server.Staff;
import server.Person; 
import server.Data;

import java.rmi.RemoteException;
import java.sql.*;


public class Company extends Enterprise implements CompanyInterface
{
	/*
	private double founds;
	private int maxStaff;
	private double maxLoanVal;
	private int staffNum;
	private int StaffID;
	private double loanVal;
	private double maxInvestFounds;
	private int equipmentLev=1;//研究设备等级,初始为1
	private int maxEquipmentLev=5;//设备等级上限
	private double maxMarketResFounds;

	*/
	//private Staff[] staff;
	private Data data;
	
	public Company()throws RemoteException//int ID, String name, int locationNum,int bankAccountID,double founds,int maxStaff,double maxLoanAmount)
	{
	/*	super(ID, name, locationNum, bankAccountID);
		this.founds=founds;
		this.maxStaff=maxStaff;
		this.maxLoanAmount=maxLoanAmount;
	*/	
		data=new Data();
	}
	
	public int getStaffID(int id)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int staffID=0;
		
		sql="select * from company where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				staffID=rs.getInt("staffID");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return staffID;
	}
	public double getFounds(int id)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		double founds=0;
		
		sql="select * from company where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
			founds=rs.getDouble("founds");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return founds;
	}
	
	public int getMaxStaff(int id)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int maxStaff=0;
		
		sql="select * from company where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
			maxStaff=rs.getInt("maxStaff");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
			
		return maxStaff;
	}
	
	public int getStaffNum(int id)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int staffNum=0;
		
		sql="select * from company where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				staffNum=rs.getInt("staffNum");
		}
			} catch (SQLException e) {

			e.printStackTrace();
			}
			
		return staffNum;
	}
	
	public double getMaxLoanVal(int id)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		double maxLoanVal=0;
		
		sql="select * from company where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				maxLoanVal=rs.getInt("maxLoanVal");
		}
			} catch (SQLException e) {

			e.printStackTrace();
			}
			
		return maxLoanVal;
	}


	public double getLoanVal(int id)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		double loanVal=0;
		
		sql="select * from company where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				loanVal=rs.getInt("loanVal");
		}
			} catch (SQLException e) {
			e.printStackTrace();
			}
			
		return loanVal;
	}
	

	public boolean setFounds(int id,double founds)throws RemoteException
	{
		String sql;
		int re=0;
		
		sql="update company set founds = "+founds+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
		
	}
	

	public boolean setMaxStaff(int id,int maxStaff)throws RemoteException
	{
		String sql;
		int re=0;
		
		sql="update company set maxStaff = "+maxStaff+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
		
	}
	
	public boolean setStaffNum(int id,int staffNum)throws RemoteException
	{
		String sql;
		int re=0;
		
		sql="update company set staffNum = "+staffNum+" where id="+id;
		re=data.update(sql);
		
		if(re==1){

			return true;
		}
		return false;
	}
	
	public boolean setMaxLoanAmount(int id,double maxLoanAmount)throws RemoteException
	{
		String sql;
		int re=0;
		
		sql="update company set maxLoanVal = "+maxLoanAmount+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}

	public boolean setLoanVal(int id,double loanVal)throws RemoteException
	{
		String sql;
		int re=0;
		
		sql="update company set loanVal = "+loanVal+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
		

	public boolean isNotStaffFull(int id)throws RemoteException
	{
		if(getStaffNum(id)<=getMaxStaff(id))
			return true;
		else
			return false;
	}
	

	public boolean loan(int id,double loanVal)throws RemoteException
	{
		if(loanVal<=getMaxLoanVal(id))
		{
			setLoanVal(id,getLoanVal(id)+loanVal);
			return true;
		}
		else 
		{
			//锟斤拷示锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
			return false;
		}
		
	}
	
	public boolean reloan(int id,double reloanVal)throws RemoteException
	{
		if(reloanVal<=getFounds(id))
		{
			//锟斤拷锟斤拷锟斤拷锟斤拷锟洁函锟斤拷庸锟剿撅拷嘶锟斤拷诳锟角拷锟街滴猺eloanVal
			setLoanVal(id,getLoanVal(id)-reloanVal);
			return true;
		}
		else
		{
			//锟斤拷示锟斤拷锟斤拷司锟绞斤拷锟姐”
			return false;
		}
	}
	
/*	//锟斤拷聘
	public boolean recruit(int id,Staff staffinfo,Person per)
	{
		if(isNotStaffFull(id))
		{

			per.apply(id, staffinfo.getPost(id));//锟斤拷锟剿碉拷job锟侥成改癸拷司锟斤拷撞锟皆憋拷锟�
			setStaffNum(id,getStaffNum(id)+1);//staffNum++;
			return true;
		}
		else
		{
			//锟斤拷示锟斤拷员锟斤拷锟斤拷锟斤拷谢谢锟斤拷
			return false;
		}
	}
*/
	//锟斤拷职
	/*public boolean resign(int id,Person per)
	{
		per.resignate(id, "锟斤拷");
		setStaffNum(id,getStaffNum(id)-1);//staffNum--;
		if(per.getJob(id).equals("锟斤拷"))
			//锟斤拷示同锟斤拷锟街�
			return true;
		else
		{
			//锟斤拷示锟斤拷锟斤拷
			return false;
		}
	}
	*/
	
	//盈锟斤拷
	public void profit(int id,double pro)throws RemoteException
	{
		
		setFounds(id,getFounds(id)+pro);//this.founds=this.founds+pro;
		//锟睫改癸拷司锟剿伙拷锟绞斤拷
	}
	
	//支锟斤拷薪水
	public boolean paySalary(int id,int staffID,double salary)throws RemoteException
	{
		if(salary<=getFounds(id))
		{
			//锟斤拷司锟剿伙拷锟斤拷钱
			//锟斤拷锟剿碉拷锟剿伙拷锟斤拷转锟斤拷salary
			return true;
		}
		else
		{
			//锟斤拷示锟斤拷锟斤拷锟斤拷锟绞斤拷锟姐”
			return false;
		}
	}
/*	
	//锟斤拷锟姐工锟斤拷
	public boolean workSettle(int id,Staff staffinfo,Staff staffinfo1,Person per)
	{
		if(staffinfo.getConsumHp(id,per.getJob(id))<per.getHp(id))//锟叫讹拷hp锟角凤拷透支
		{
			per.hpChanges(id,staffinfo.getConsumHp(id,per.getJob(id)),'-');
			//锟斤拷锟斤拷锟斤拷锟街�
			if(staffinfo.getConsumHap(id,per.getJob(id))<per.getHap(id))//锟叫讹拷hap锟角凤拷透支
			{
				per.hapChanges(id,staffinfo.getConsumHap(id,per.getJob(id)),'-');
				per.expChanges(id, staffinfo.getExp(id,per.getJob(id)), '+');
				
				while(staffinfo.isPromoted(per, id))
					per.apply(id, staffinfo1.getPost(id));
					return true;	
				//锟斤拷锟斤拷锟斤拷锟街�
				//锟斤拷锟剿碉拷锟斤拷锟斤拷锟剿伙拷转锟斤拷锟街霸眘alary
				//锟斤拷锟斤拷exp
			}
			else return false;
		}
		else return false;
	}
	*/
	
	public int getSize() throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int sum=0;
		
		sql="select count(*) as sum from company";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				sum=rs.getInt("sum");
		}
			} catch (SQLException e) {

			e.printStackTrace();
			}
			
		return sum;
	}
	
	

}
